package com.hmkj.core.mapper.account;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.account.AccountCashModel;
import com.hmkj.core.po.account.AccountCash;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 提现记录相关Dao接口<br>
 *
 * @author eproo
 * @date 2018-07-05 09:50:54
 */
public interface AccountCashMapper extends GenericMapper<AccountCash, Integer> {

    @Select("SELECT t.*,u.user_name AS 'u.userName',u.mobile_phone AS 'u.mobilePhone' FROM biz_account_cash t LEFT JOIN biz_user u ON u.id = t.user_id WHERE 1=1 ${whereSql} ${sortSql} limit ${nowPage}, ${pageSize} ")
    List<AccountCashModel> findListBySqlJoin(Map<String, Object> params);

    @Select("SELECT count(1) FROM biz_account_cash t LEFT JOIN biz_user u ON u.id = t.user_id WHERE 1=1 ${whereSql}")
    long countBySqlJoin(Map<String, Object> params);

    /**
     * 根据订单号查询提现订单
     *
     * @param orderNo 订单号
     * @return
     */
    @Select("SELECT * FROM biz_account_cash WHERE 1=1 and order_no = #{orderNo} and del_flag = 1")
    AccountCash selectByOrderNo(@Param(value = "orderNo") String orderNo);

    /**
     * 前台提现记录列表
     *
     * @param param
     * @return
     */
    @Select(" <script> SELECT t.* " +
            "FROM biz_account_cash t WHERE t.user_id = #{userId}" +
            "<if test=\"type != null and type != ''\"> AND t.`type` = #{type} </if>" +
            "ORDER BY t.create_time DESC </script>")
    List<AccountCash> findListForCashList(Map<String, Object> param);

    /**
     * 前台提现记录列表
     *
     * @param param
     * @return
     */
    @Select(" <script> SELECT count(*) " +
            "FROM biz_account_cash t WHERE t.user_id = #{userId}" +
            "<if test=\"type != null and type != ''\"> AND t.`type` = #{type} </if>" +
            "ORDER BY t.create_time DESC </script>")
    Integer findCountForCashList(Map<String, Object> param);

    /**
     * 更新提现状态
     *
     * @param id        提现ID
     * @param newStatus 新状态
     * @param orgStatus 原状态
     * @return
     */
    @Update("UPDATE biz_account_cash SET status = #{newStatus} WHERE id = #{id} AND status = #{orgStatus} AND del_flag = 1")
    int updateCashStatus(@Param(value = "id") Integer id, @Param(value = "newStatus") Integer newStatus, @Param(value = "orgStatus") Integer orgStatus);

    /**
     * 根据状态统计提现金额
     * @param userId
     * @return
     */
    @Select("<script> SELECT SUM(t.amount) FROM biz_account_cash t WHERE t.user_id = #{userId} AND t.`status` IN(2,3,4) AND to_days(t.create_time) = to_days(now()) AND t.del_flag = 1 </script>")
    BigDecimal selectTodayCashAmount(@Param(value = "userId") Integer userId);

    /**
     * 根据状态统计提现笔数
     * @param userId
     * @return
     */
    @Select("<script> SELECT COUNT(1) FROM biz_account_cash t WHERE t.user_id = #{userId} AND t.`status` IN(1,2,3,4) AND to_days(t.create_time) = to_days(now()) AND t.del_flag = 1 </script>")
    Integer selectTodayCashCount(@Param(value = "userId") Integer userId);

}
